/*
 * GeekOS C code entry point
 * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
 * Copyright (c) 2004, Iulian Neamtiu <neamtiu@cs.umd.edu>
 * $Revision: 1.51 $
 * 
 * This is free software.  You are permitted to use,
 * redistribute, and modify it as specified in the file "COPYING".
 */

#include <geekos/bootinfo.h>
#include <geekos/string.h>
#include <geekos/screen.h>
#include <geekos/mem.h>
#include <geekos/crc32.h>
#include <geekos/tss.h>
#include <geekos/int.h>
#include <geekos/kthread.h>
#include <geekos/trap.h>
#include <geekos/timer.h>
#include <geekos/keyboard.h>

void hello (ulong_t arg);

/*
 * Kernel C code entry point.
 * Initializes kernel subsystems, mounts filesystems,
 * and spawns init process.
 */
void Main(struct Boot_Info* bootInfo)
{

	struct Kernel_Thread *new_thread = NULL;

    Init_BSS();
    Init_Screen();
    Init_Mem(bootInfo);
    Init_CRC32();
    Init_TSS();
    Init_Interrupts();
    Init_Scheduler();
    Init_Traps();
    Init_Timer();
    Init_Keyboard();


    Set_Current_Attr(ATTRIB(BLACK, GREEN|BRIGHT));
    Print("Welcome to GeekOS!\n");
    Set_Current_Attr(ATTRIB(BLACK, GRAY));

	/* creamos nuevo thread para imprimir "hello world" */
	new_thread = Start_Kernel_Thread(hello, 0, PRIORITY_NORMAL, false);
	KASSERT(new_thread != NULL);
    Exit(0); /* salimos del thread creado */
}

void hello (ulong_t arg)
{
	Keycode new_code = 0;
	bool exit = false;
	int i = 1;
	
	/* queremos entrar a hello sii se hace desde Main conr arg=0 */
	KASSERT (arg == 0);
	
	Print("Hello world!\n");
	
	/* el keycode de ctrl+d = 4064 */
	
	while (!exit)
	{
		new_code = Wait_For_Key();
		exit = (new_code == 0x4064);
		
		/* Wait_For_Key() registra 2 eventos: la presion de teclas y cuando
		 * se las suelta. Sólo debemos imprimir la tecla en el momento de la
		 * presión */
		if ((i%2) == 1 && (!exit))
			Print("%c\n", (char) new_code);
		
		i+=1;
	}
}
	
	









